// pages/creat/creat.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_yes:true,
    no_resource:true,
	tip:30,
	title:"",//标题 
	pnum:0,//群成员数
	upnum:0,//申请次数
	seanum:0, //访问量
	name:"",    //姓名
	about:"", //简介
	selected:true,  //tab选项卡控制器
	selected1: false,
	mon:0, //价钱
	 items: [{ name: '1', checked: 'true' },]

  },
  checkboxChange:function(e){
    console.log(e)
    if(e.detail.value[0]==1){
        this.setData({
          is_yes:true
        })
    }else{
      this.setData({
        is_yes: false
      })
    }
    console.log(this.data.is_yes)
  },
  selected: function (e) {
	  this.setData({
		  selected1: false,
		  selected: true
	  })
  },
  selected1: function (e) {
	  this.setData({
		  selected: false,
		  selected1: true
	  })
  },
  pay:function(){
      var that=this
      var wx_id=wx.getStorageSync('uid')
      // var id = that.data.groupid
      var is_yes=this.data.is_yes
      var mon=this.data.mon
      var grouper_vip = that.data.grouper_vip
      if(that.data.is_auth){
          if(that.data.is_card){
            if (is_yes == true) {
              if (grouper_vip==1){
                wx.showLoading({ title: '加载中...', })
                wx.login({
                  success: function (res) {
                    console.log(res.code)
                    if (res.code) {
                      //发起网络请求
                      wx.request({
                        url: app.globalData.url + 'xcx/Paying/' + res.code + '/' + mon + '/group' + '/' + that.data.grouper_id,
                        //  data:{
                        //    code:res.code,
                        //     money:that.data.mon  grouper_id
                        //  },
                        method: "get",
                        success: function (res) {
                          console.log(res)

                          wx.hideLoading()
                          wx.requestPayment({
                            'timeStamp': res.data.timeStamp,
                            'nonceStr': res.data.nonceStr,
                            'package': res.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.paySign,
                            'success': function (res) {
                              console.log(res)
                              if (res.errMsg == 'requestPayment:ok') {
                                wx.request({
                                  url: app.globalData.url + 'xcx/appyGroup',
                                  data: {
                                    now_group_name:that.data.title,
                                    groupid: that.data.groupid,
                                    wx_id: wx_id
                                  },
                                  method: "post",
                                  success: function (res) {
                                    console.log(res)
                                    if (res.data.code == 10000) {
                                      wx.showToast({
                                        title: '申请成功',
                                        icon: 'success',
                                        duration: 1000

                                      })

                                      that.setData({
                                        is_group: true
                                      })
                                      that.load()
                                      wx.setStorageSync('group_id' + that.data.groupid, that.data.groupid)

                                    }

                                  }

                                })

                                wx.request({
                                  url: app.globalData.url + 'xcx/groupFieldAddone',
                                  data: {
                                    filedname: 'apply',
                                    groupid: that.data.groupid
                                  },
                                  method: "post",
                                  success: function (res) {
                                    console.log(res)
                                    that.setData({
                                      pnum: that.data.pnum + 1
                                    })
                                  }
                                })

                                wx.request({
                                  url: app.globalData.url + 'xcx/groupFieldAddone',
                                  data: {
                                    filedname: 'member',
                                    groupid: that.data.groupid
                                  },
                                  method: "post",
                                  success: function (res) {
                                    console.log(res)
                                    that.setData({
                                      upnum: that.data.upnum + 1
                                    })
                                  }
                                })




                              }



                            },
                            'fail': function (res) {

                            }
                          })





                        }
                      })
                    } else {
                      console.log('登录失败！' + res.errMsg)
                    }
                  }
                });


              }else{
                wx.showToast({
                  title: '群已到期',
                  icon: 'none',
                  duration: 1000
                })

              }

            } else {
              wx.showToast({
                title: '请勾选用户协议',
                icon: 'none',
                duration: 1000
              })
            }
          }else{
            wx.showToast({
              title: '请先进入我的主页创建名片',
              icon: 'none',
              duration: 1000
            })
          }

      }else{

        wx.showToast({
          title: '请先进入我的主页',
          icon: 'none',
          duration: 1000
        })


      }




    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var id = wx.getStorageSync('uid')
    var that = this
    if (id) {
      this.setData({
        is_auth: true
      })
      wx.request({
        url: app.globalData.url + 'xcx/getNowCard/' + id,
        data: {},
        method: "get",
        success: function (res) {
          console.log(res)
          if (res.data.code == 10000) {
            that.setData({
              is_card: true
            })
          } else {
            that.setData({
              is_card: false
            })
          }
        }
      })
    } else {
      this.setData({
        is_auth: false
      })
    }



    console.log(options.id)
    var id = options.id
    var wx_id=wx.getStorageSync('uid')


    
    var group_id = wx.getStorageSync('group_id' + id)
    
    if (group_id == id) {
      this.setData({
        is_group: true
      })
    } else {
      this.setData({
        is_group: false
      })
    }
   





    this.setData({
      groupid:id
    })
    var that = this
    wx.request({
      url: app.globalData.url + 'xcx/groupDetail/'+id,
      data: {},
      method: "get",
      success: function (res) {
        console.log(res)
        if (wx_id == res.data.data.user_wx_id){
            that.setData({
              is_grouper: true
            })
        }else{
          that.setData({
            is_grouper: false
          })
          
        }
        console.log(that.data.is_grouper)
        if (res.data.data) {
          that.setData({
            wx_er_wei_ma_img: res.data.data.wx_er_wei_ma_img,
            wx_qun_er_wei_ma_img: res.data.data.wx_qun_er_wei_ma_img,
            title: res.data.data.group_name,
            name: res.data.data.grouper_name,
            headimg: res.data.data.wx_header_img,
            pnum: res.data.data.group_member_number,
            upnum: res.data.data.apply_number,
            seanum: res.data.data.visited_number,
            about: res.data.data. group_introduce,
            mon: res.data.data.group_money,
            grouper_id: res.data.data.user_wx_id,
            grouper_vip: res.data.data.grouper_vip
          })
        }

      }
    })

    wx.request({
      url: app.globalData.url + 'xcx/groupFieldAddone',
      data: {
        filedname: 'visited',
        groupid:id
        },
      method: "post",
      success: function (res) {
        // console.log(res)
        // var seanum = that.data.seanum
        // that.setData({
        //   seanum: seanum
        // })
      }
    })

    wx.request({
      url: app.globalData.url +'xcx/getAllgroupers/'+id,
      data:{

      },
      method:"get",
      success:function(res){
        console.log(res)
        if (res.data.data){
          that.setData({
            group_people: res.data.data
          })
        }
        
      }
    })

  
    

      var id = that.data.groupid

      wx.request({
        url: app.globalData.url + 'xcx/getGroupResource/' + id,
        method: "get",
        success: function (res) {
          console.log(res)
          if (res.data.data) {
            for (var i = 0; i < res.data.data.length; i++) {
              res.data.data[i].info_desc = res.data.data[i].info_desc.substring(0, 40)
            }
            that.setData({
              ResourceData: res.data.data
            })
          }else{
            that.setData({
              no_resource:false
            })
            
          }
        }
      })




     
    
    


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  load:function(){
    var that=this
    var id = that.data.groupid
    wx.request({
      url: app.globalData.url + 'xcx/getAllgroupers/' + id,
      data: {

      },
      method: "get",
      success: function (res) {
        console.log(res)
        if (res.data.data) {
          that.setData({
            group_people: res.data.data
          })
        }

      }
    })




    

    wx.request({
      url: app.globalData.url + 'xcx/getGroupResource/' + id,
      method: "get",
      success: function (res) {
        console.log(res)
        if (res.data.data) {
          that.setData({
            ResourceData: res.data.data
          })
        }else{
          that.setData({
            no_resource: false
          })
        }
      }
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
   


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)



    }

    var that = this




    return {
      title: '用名片打动世界',
      path: '',
      imageUrl: this.data.card_img,
      success: function (res) {
       

       
      },
      fail: function (res) {
       
      }
    } 

  },
 
})